import coordTransform_utils as cod
from urllib import request
def wgs2gcj():
    wgsfile=open('chinapt.txt')
    wgsfile.readline()
    gcjfile=open('gcjfile.txt','w')
    a='s'
    while True:
        a=wgsfile.readline()
        a=a.replace('\n','')
        if a=='':
            break
        l=a.split('\t')
        webhttp='http://restapi.amap.com/v3/assistant/coordinate/convert?key=c3542411d4890c56b5a287ef7fc88a42&locations='
        end='&coordsys=gps'
        visit=webhttp+l[1]+','+l[2]+end
        s=request.urlopen(visit)
        txt=s.read()
        dic=eval(txt.decode('utf-8'))
        print(dic['locations'])
        gcjfile.write(dic['locations']+'\n')
        gcjfile.flush()
def gcj2wgs():
    gcjfile=open('gcjfile.txt')
    wgsfile=open('newchinapt.txt','w')
    while True:
        a=gcjfile.readline()
        if a=='':
            break
        l=a.split(',')
        print(l[0]+","+l[1])
        l=cod.gcj02_to_wgs84(float(l[0]),float(l[1]))
        wgsfile.write(str(l[0])+","+str(l[1])+'\n')
        wgsfile.flush()

def wgs2bd09():
    wgsfile=open('chinapt.txt')
    wgsfile.readline()
    gcjfile=open('bdfile.txt','w')
    a='s'
    while True:
        a=wgsfile.readline()
        a=a.replace('\n','')
        if a=='':
            break
        l=a.split('\t')
        webhttp='http://api.map.baidu.com/geoconv/v1/?coords='
        end='&from=1&to=5&ak=aKoLKCoGFhsfBzcmcSFdz6NWejLVLS76'
        visit=webhttp+l[1]+','+l[2]+end
        s=request.urlopen(visit)
        txt=s.read()
        dic=eval(txt.decode('utf-8'))
        print(str(dic['result'][0]['x'])+','+str(dic['result'][0]['y']))
        gcjfile.write(str(dic['result'][0]['x'])+','+str(dic['result'][0]['y'])+'\n')
        gcjfile.flush()

def bd092wgs():
    gcjfile=open('bdfile.txt')
    wgsfile=open('newchinapt_bd.txt','w')
    while True:
        a=gcjfile.readline()
        if a=='':
            break
        l=a.split(',')
        print(l[0]+","+l[1])
        l=cod.bd09_to_wgs84(float(l[0]),float(l[1]))
        wgsfile.write(str(l[0])+","+str(l[1])+'\n')
        wgsfile.flush()
